package fr.ralmn.OgameDroid.list.Adapter;

import java.util.List;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import fr.ralmn.OgameDroid.R;
import fr.ralmn.OgameDroid.Object.Technologie;
	
public class TechoArrayAdapter extends ArrayAdapter<Technologie> {

	public class ViewHolder {
		public TextView nameView;
		public ImageView iconeView;
	}

	private List<Technologie> technos;
	private LayoutInflater mInflater;
	private Context mContext;

	public TechoArrayAdapter(Context context, List<Technologie> techno) {
		super(context, R.id.t_list_name, R.id.t_list_img, techno);
		technos = techno;
		mInflater = LayoutInflater.from(context);
		mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout layoutItem;
		if (convertView == null) {
			layoutItem = (LinearLayout) mInflater.inflate(R.layout.techno_list,
					parent, false);
		} else {
			layoutItem = (LinearLayout) convertView;
		}

		TextView name = (TextView) layoutItem.findViewById(R.id.t_list_name);
		ImageView img = (ImageView) layoutItem.findViewById(R.id.t_list_img);

		name.setText(technos.get(position).getName());
		img.setImageResource(technos.get(position).getIcon());

		int height_in_pixels = name.getLineCount() * name.getLineHeight(); //approx height text
		return layoutItem;
	}

	@Override
	public int getCount() {
		return technos.size();
	}

	@Override
	public Technologie getItem(int position) {
		return technos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static void autoScaleTextViewTextToHeight(final TextView tv, String s)
    {       
        float currentWidth=tv.getPaint().measureText(s);
        int scalingFactor = 0;
        final int characters = s.length();
        //scale based on # of characters in the string
        if(characters<5)
        {
            scalingFactor = 1;
        }
        else if(characters>=5 && characters<10)
        {
            scalingFactor = 2;
        }
        else if(characters>=10 && characters<15)
        {
            scalingFactor = 3;
        }
        else if(characters>=15 && characters<20)
        {
            scalingFactor = 3;
        }
        else if(characters>=20 && characters<25)
        {
            scalingFactor = 3;
        }
        else if(characters>=25 && characters<30)
        {
            scalingFactor = 3;
        }
        else if(characters>=30 && characters<35)
        {
            scalingFactor = 3;
        }
        else if(characters>=35 && characters<40)
        {
            scalingFactor = 3;
        }
        else if(characters>=40 && characters<45)
        {
            scalingFactor = 3;
        }
        else if(characters>=45 && characters<50)
        {
            scalingFactor = 3;
        }
        else if(characters>=50 && characters<55)
        {
            scalingFactor = 3;
        }
        else if(characters>=55 && characters<60)
        {
            scalingFactor = 3;
        }
        else if(characters>=60 && characters<65)
        {
            scalingFactor = 3;
        }
        else if(characters>=65 && characters<70)
        {
            scalingFactor = 3;
        }
        else if(characters>=70 && characters<75)
        {
            scalingFactor = 3;
        }
        else if(characters>=75)
        {
            scalingFactor = 5;
        }

        //System.out.println(((int)Math.ceil(currentWidth)/tv.getWidth()+scalingFactor));
        //the +scalingFactor is important... increase this if nec. later
        while((((int)Math.ceil(currentWidth)/tv.getWidth()+scalingFactor)*tv.getTextSize())<tv.getHeight())
        {
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, tv.getTextSize()+0.25f);
            currentWidth=tv.getPaint().measureText(s);
            //System.out.println(((int)Math.ceil(currentWidth)/tv.getWidth()+scalingFactor));
        }

        tv.setText(s);
    }
	
}
